package database

import (
	"app-box/config"
	"fmt"
	"log"

	"gorm.io/driver/mysql"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

var DefaultDB *gorm.DB

func InitDB() {
	var err error = nil
	switch config.Server.DBType {
	case "MySQL":
		dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
			config.MySQL.UserName, config.MySQL.Password, config.MySQL.Address, config.MySQL.DataBase)
		DefaultDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	case "SQLite":
		DefaultDB, err = gorm.Open(sqlite.Open(config.SQLite.DBPath), &gorm.Config{})
	}
	if err != nil {
		log.Fatal(err)
	}
}
